<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    /**
     * 
     * Delete all files and subdiretories in diretory and directory itself
     */
    function delete_folder($dir, $virtual = false) {
        $ds = DIRECTORY_SEPARATOR;
        $dir = $virtual ? realpath($dir) : $dir;
        $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir;
        if (is_dir($dir) && $handle = opendir($dir)) {
            while ($file = readdir($handle)) {
                if ($file == '.' || $file == '..') {
                    continue;
                } elseif (is_dir($dir . $ds . $file)) {
                    delete_folder($dir . $ds . $file);
                } else {
                    unlink($dir . $ds . $file);
                }
            }
            closedir($handle);
            rmdir($dir);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 
     * Create new diretory
     * @param name
     * @param path
     */
    function create_folder($name, $path = "") {
        $path_name = "./" . $path . "/" . $name;
        if (!file_exists($path_name)) {
            if (!mkdir($path_name, 0777)) {
                return FALSE;
            } else
                return TRUE;
        } else
            return TRUE;
    }

    /**
     * Delete file
     * 
     * @param name
     * @param path
     */
    function delete_file($URL, $filename) {

        if (file_exists($URL . $filename))
            unlink($URL . $filename);
        else
            return 'File not exist! Path: ' . $URL . $filename;
    }
    
     function delete_file_cms($URL, $filename) {

        if (file_exists($URL . $filename))
            unlink($URL . $filename);
        else
            return 'File not exist! Path: ' . $URL . $filename;
    }

    function get_extension($filename, $i) {
        $pices = explode('.', $filename);
        
        return $pices[count($pices) - 1];
    }
    